package com.kaizzi.data.jdl.helper;

import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.common.utils.StringUtil;
import com.kaizzi.data.jdl.model.ExcelModel;
import com.kaizzi.data.jdl.model.Page12Model;
import com.kaizzi.data.jdl.service.CrmMemberService;
import com.kaizzi.data.jdl.service.OrderListService;
import com.kaizzi.data.jdl.service.TaskLogsService;
import lombok.Data;
import org.apache.poi.ss.formula.functions.T;
import org.aspectj.lang.annotation.DeclareAnnotation;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/15
 */
@Component
public class PageData12 {

    @Resource
    private CrmMemberService crmMemberService;
    @Resource
    private TaskLogsService taskLogsService;
    @Resource
    private OrderListService orderListService;

    public String getData(ExcelModel model){

        Page12 page12 = new Page12();
        List<TempData12> tempData12s = new ArrayList<>();

        List<String> monthDate = DateUtil.getMonthDate(0);
        List<String> ytdDay = DateUtil.getYtdDay();
        //总用户数
        TempData12 totalUser = getTotalUser(monthDate,ytdDay);
        tempData12s.add(totalUser);
        //wes
        TempData12 wesCount = getWesCount(monthDate,ytdDay);
        tempData12s.add(wesCount);
        //小程序分享数据
        String shareUsers = "";
        String shareUsers1 = "";
        String shareTimes = "";
        String shareTimes1 = "";
        List<Page12Model> page12s = model.getPage12s();
        for (Page12Model pageModel : page12s) {
            TempData12 tempData12 = new TempData12();
            if (pageModel.getName().contains("人")){
                shareUsers = pageModel.getMtd();
                shareUsers1 = pageModel.getYtd();
                tempData12.setName("分享人数");
                tempData12.setMtd(StringUtil.pointFormat(Double.parseDouble(shareUsers.replaceAll(",",""))));
                tempData12.setYtd(StringUtil.pointFormat(Double.parseDouble(shareUsers1.replaceAll(",",""))));
                tempData12s.add(tempData12);
                continue;
            }
            shareTimes = pageModel.getMtd();
            shareTimes1 = pageModel.getYtd();
            tempData12.setName("分享次数");
            tempData12.setMtd(StringUtil.pointFormat(Double.parseDouble(shareTimes.replaceAll(",",""))));
            tempData12.setYtd(StringUtil.pointFormat(Double.parseDouble(shareTimes1.replaceAll(",",""))));
            tempData12s.add(tempData12);
        }
        TempData12 exchangeUsers = getExchangeUsers(monthDate,ytdDay);
        tempData12s.add(exchangeUsers);
        TempData12 expendPoints = getExpendPoints(monthDate,ytdDay);
        tempData12s.add(expendPoints);
        TempData12 points = getPoints(monthDate,ytdDay);
        tempData12s.add(points);

        page12.setDataPeriod(DateUtil.getFirstDay2LastSunday());
        page12.setBeens(tempData12s);

        return JsonUtil.serialize(page12);
    }

    //总用户数
    private TempData12 getTotalUser(List<String> dateList,List<String> dateList1){
        long userCount = crmMemberService.getUserCount(dateList.get(0), dateList.get(1), "");
        long userCount1 = crmMemberService.getUserCount(dateList1.get(0), dateList1.get(1), "");
        TempData12 tempData12 = new TempData12();
        tempData12.setName("总用户数");
        tempData12.setMtd(StringUtil.pointFormat(userCount));
        tempData12.setYtd(StringUtil.pointFormat(userCount1));
        return tempData12;
    }

    //wes
    private TempData12 getWesCount(List<String> dateList,List<String> dateList1){
        long wes = crmMemberService.getWes(dateList.get(0), dateList.get(1));
        long wes1 = crmMemberService.getWes(dateList1.get(0), dateList1.get(1));
        TempData12 tempData12 = new TempData12();
        tempData12.setName("wes");
        tempData12.setMtd(StringUtil.pointFormat(wes));
        tempData12.setYtd(StringUtil.pointFormat(wes1));
        return tempData12;
    }

    //积分-兑换人数
    private TempData12 getExchangeUsers(List<String> dateList,List<String> dateList1){
        long exchangeUser = orderListService.getExchangeUser(dateList.get(0), dateList.get(1));
        long exchangeUser1 = orderListService.getExchangeUser(dateList1.get(0), dateList1.get(1));
        TempData12 tempData12 = new TempData12();
        tempData12.setName("兑换人数");
        tempData12.setMtd(StringUtil.pointFormat(exchangeUser));
        tempData12.setYtd(StringUtil.pointFormat(exchangeUser1));
        return tempData12;
    }

    //积分-消耗总分
    private TempData12 getExpendPoints(List<String> dateList,List<String> dateList1){
        long expendPoints = taskLogsService.getExpendPoints(dateList.get(0), dateList.get(1));
        long expendPoints1 = taskLogsService.getExpendPoints(dateList1.get(0), dateList1.get(1));
        TempData12 tempData12 = new TempData12();
        tempData12.setName("消耗总分");
        tempData12.setMtd(StringUtil.pointFormat(expendPoints));
        tempData12.setYtd(StringUtil.pointFormat(expendPoints1));
        return tempData12;
    }

    //积分-获取总分
    private TempData12 getPoints(List<String> dateList,List<String> dateList1){
        long points = taskLogsService.getPoints(dateList.get(0), dateList.get(1));
        long points1 = taskLogsService.getPoints(dateList1.get(0), dateList1.get(1));
        TempData12 tempData12 = new TempData12();
        tempData12.setName("获取总分");
        tempData12.setMtd(StringUtil.pointFormat(points));
        tempData12.setYtd(StringUtil.pointFormat(points1));
        return tempData12;
    }

    @Data
    private static class Page12 {
        private String dataPeriod;
        private String title;
        private List<TempData12> beens;
    }

    @Data
    private static class TempData12{
        private String name;
        private String mtd;
        private String ytd;
    }

}
